package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 数据库工具类
 * @author 梁
 *
 */
public class DbUtil {
	
	private static final String USER = "root";
	private static final String PASSWORD = "123456";
	private static final String PARAMETER8 = "characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8"; 
	private static final String URL = "jdbc:mysql://localhost:3306/db_book?"+ PARAMETER8;  
	private static Connection conn=null;
	
	/**
	 * 获得数据库连接
	 * @return java.sql.Connection实例
	 */
	public Connection getCon() throws Exception{
		Class.forName("com.mysql.cj.jdbc.Driver"); 
		conn = DriverManager.getConnection(URL, USER, PASSWORD);
		return conn;
	}
	
	
	/**
	 * 关闭数据库连接
	 */
	public void closeCon(Connection con)throws Exception{
		if(con!=null) {
			con.close();
		}
	}
	
	
	 
	/**
	 * 测试数据库是否连接成功
	 * @author 梁
	 *
	 */
	public static void main(String[] args) {
		DbUtil dbUtil=new DbUtil();
		try {
			dbUtil.getCon();
			System.out.println("数据库连接成功");
		}catch(Exception e) {
			e.printStackTrace();
			System.out.println("数据库连接失败");
		}
		
		
	}
}
